// 获取获奖次数
(function () {
    let number = 5;
    let numberTxt = document.querySelector(".number-txt");
    let startBtn = document.querySelector(".controller-right");
    let prizeLists = document.querySelectorAll(".prize-list");
    let dialog = document.querySelector(".mask-dialog");
    let dialogContent = document.querySelector(".mask-dialog .mask-content .content");
    let closeBtn = document.querySelector(".mask-dialog .mask-content .head .close");
    let againBtn = document.querySelector(".mask-dialog .mask-content .confirm-btn");
    let index = -1;
    let currrentIndex = null;
    let timer = null;

    function init() {
        numberTxt.innerHTML = number;
        initEvent();
    }

    let initEvent = function () {
        startBtn.addEventListener('click', onStartBtnClick);
        closeBtn.addEventListener('click', onCloseDialog);
        againBtn.addEventListener('click', onAgainClick)
    }

    let onStartBtnClick = function () {
        ranGame();

    }

    let openDialog = function () {
        numberTxt.innerHTML = --number;
        let prizeContent = prizeLists[currrentIndex].innerHTML;
        if (number === 0) {
            againBtn.innerHTML = "确定";
        }
        if (currrentIndex !== 4) {
            dialogContent.innerHTML = '恭喜您获得' + prizeContent;
        } else {
            dialogContent.innerHTML = '谢谢参与'
        }
        dialog.style.display = 'block';
    }

    let onCloseDialog = function () {
        dialog.style.display = 'none';
    }

    let onAgainClick = function () {
        dialog.style.display = 'none';
        ranGame();
    }

    let ranGame = function () {
        if (number === 0 || timer) {
            return
        }
        let num = 3400 + Math.floor(Math.random() * 1800)
        timer = setInterval(function () {
            num -= 200;
            if (num <= 0) {
                clearInterval(timer);
                timer = null;
                openDialog();   
                return;
            }
            currrentIndex = ++index % prizeLists.length;
            prizeLists.forEach(function (node) {
                node.classList.remove('active');
            })
            prizeLists[currrentIndex].classList.add("active")
        }, 50)
    }

    init();
})()